package model

import (
	"wzgames/internal/constant"
	"wzgames/pkg/logger"
	"wzgames/pkg/remote/etcd"
	"wzgames/pkg/server/http"
	"wzgames/pkg/server/rpcx"
	"wzgames/pkg/server/ws"
)

type GBoot struct {
	Env  constant.EnvType     `yaml:"env"`
	CC   *etcd.EtcdConfig     `yaml:"cc"`
	LogC *logger.LoggerConfig `yaml:"log"`

	node   *Node
	nodeID string
}

func (gb *GBoot) Node() *Node {
	return gb.node
}

func (gb *GBoot) GetEnv() constant.EnvType {
	return gb.Env
}

func (gb *GBoot) Timezone() string {
	if gb.node == nil || gb.node.Timezone == "" {
		return "Asia/Kolkata"
	}
	return gb.node.Timezone
}

func (gb *GBoot) SetNodeID(nodeID string) {
	gb.nodeID = nodeID
}

func (gb *GBoot) GetNodeID() string {
	return gb.nodeID
}

func (gb *GBoot) SetNode(node *Node) {
	gb.node = node
	if gb.node.RpcConf != nil {
		gb.node.RpcConf.RegServs = gb.CC.Addrs
	}
}

func (gb *GBoot) CConf() *etcd.EtcdConfig {
	return gb.CC
}

func (gb *GBoot) NodeKey() string {
	return gb.node.NodeKey
}

type Node struct {
	NodeId   int64             `json:"node_id"`  // 节点ID，admin.sql#sys_nodes
	NodeKey  string            `json:"node_key"` // 节点唯一Key，constant.CcNodesRoot+"/"+NodeKey
	Name     string            `json:"name"`     // 节点名称
	Timezone string            `json:"tz"`       // 时区
	Env      constant.EnvType  `json:"env"`      // 运行环境
	Group    constant.NodeType `json:"group"`    // 分组
	Status   constant.Status   `json:"status"`

	// admin.sql#sys_servs
	HttpConf *http.HttpConfig `json:"http"` // Http服务配置
	WsConf   *ws.WsConfig     `json:"ws"`   // WebSocket服务配置
	RpcConf  *rpcx.RPCxConfig `json:"rpc"`  // RPC服务配置
}
